<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Paho MQTT C Client Library: Asynchronous subscription example</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="pahologo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Paho MQTT C Client Library
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Asynchronous subscription example </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="_m_q_t_t_client_8h.html">MQTTClient.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define ADDRESS     &quot;tcp://localhost:1883&quot;</span></div><div class="line"><span class="preprocessor">#define CLIENTID    &quot;ExampleClientSub&quot;</span></div><div class="line"><span class="preprocessor">#define TOPIC       &quot;MQTT Examples&quot;</span></div><div class="line"><span class="preprocessor">#define PAYLOAD     &quot;Hello World!&quot;</span></div><div class="line"><span class="preprocessor">#define QOS         1</span></div><div class="line"><span class="preprocessor">#define TIMEOUT     10000L</span></div><div class="line"></div><div class="line"><span class="keyword">volatile</span> <a class="code" href="_m_q_t_t_client_8h.html#a73e49030fd8b7074aa1aa45669b7fe8d">MQTTClient_deliveryToken</a> deliveredtoken;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> delivered(<span class="keywordtype">void</span> *context, <a class="code" href="_m_q_t_t_client_8h.html#a73e49030fd8b7074aa1aa45669b7fe8d">MQTTClient_deliveryToken</a> dt)</div><div class="line">{</div><div class="line">    printf(<span class="stringliteral">&quot;Message with token value %d delivery confirmed\n&quot;</span>, dt);</div><div class="line">    deliveredtoken = dt;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> msgarrvd(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *topicName, <span class="keywordtype">int</span> topicLen, <a class="code" href="struct_m_q_t_t_client__message.html">MQTTClient_message</a> *message)</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> i;</div><div class="line">    <span class="keywordtype">char</span>* payloadptr;</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;Message arrived\n&quot;</span>);</div><div class="line">    printf(<span class="stringliteral">&quot;     topic: %s\n&quot;</span>, topicName);</div><div class="line">    printf(<span class="stringliteral">&quot;   message: &quot;</span>);</div><div class="line"></div><div class="line">    payloadptr = message-&gt;<a class="code" href="struct_m_q_t_t_client__message.html#a9eff55064941fb604452abb0050ea99d">payload</a>;</div><div class="line">    <span class="keywordflow">for</span>(i=0; i&lt;message-&gt;<a class="code" href="struct_m_q_t_t_client__message.html#aa3cb44feb3ae6d11b3a4cad2d94cb33a">payloadlen</a>; i++)</div><div class="line">    {</div><div class="line">        putchar(*payloadptr++);</div><div class="line">    }</div><div class="line">    putchar(<span class="charliteral">&#39;\n&#39;</span>);</div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#abd8abde4f39d3e689029de27f7a98a65">MQTTClient_freeMessage</a>(&amp;message);</div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#a203b545c999beb6b825ec99b6aea79ab">MQTTClient_free</a>(topicName);</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> connlost(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *cause)</div><div class="line">{</div><div class="line">    printf(<span class="stringliteral">&quot;\nConnection lost\n&quot;</span>);</div><div class="line">    printf(<span class="stringliteral">&quot;     cause: %s\n&quot;</span>, cause);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#a7649e3913f9a216424d296f88a969c59">MQTTClient</a> client;</div><div class="line">    <a class="code" href="struct_m_q_t_t_client__connect_options.html">MQTTClient_connectOptions</a> conn_opts = <a class="code" href="_m_q_t_t_client_8h.html#aefd7c865f2641c8155b763fdf3061c25">MQTTClient_connectOptions_initializer</a>;</div><div class="line">    <span class="keywordtype">int</span> rc;</div><div class="line">    <span class="keywordtype">int</span> ch;</div><div class="line"></div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#a9a0518d9ca924d12c1329dbe3de5f2b6">MQTTClient_create</a>(&amp;client, ADDRESS, CLIENTID,</div><div class="line">        <a class="code" href="_m_q_t_t_client_persistence_8h.html#ae01e089313a65ac4661ed216b6ac00fa">MQTTCLIENT_PERSISTENCE_NONE</a>, NULL);</div><div class="line">    conn_opts.<a class="code" href="struct_m_q_t_t_client__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;</div><div class="line">    conn_opts.<a class="code" href="struct_m_q_t_t_client__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;</div><div class="line"></div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#aad27d07782991a4937ebf2f39a021f83">MQTTClient_setCallbacks</a>(client, NULL, connlost, msgarrvd, delivered);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_client_8h.html#aaa8ae61cd65c9dc0846df10122d7bd4e">MQTTClient_connect</a>(client, &amp;conn_opts)) != <a class="code" href="_m_q_t_t_client_8h.html#acba095704d79e5a1996389fa26203f73">MQTTCLIENT_SUCCESS</a>)</div><div class="line">    {</div><div class="line">        printf(<span class="stringliteral">&quot;Failed to connect, return code %d\n&quot;</span>, rc);</div><div class="line">        exit(EXIT_FAILURE);</div><div class="line">    }</div><div class="line">    printf(<span class="stringliteral">&quot;Subscribing to topic %s\nfor client %s using QoS%d\n\n&quot;</span></div><div class="line">           <span class="stringliteral">&quot;Press Q&lt;Enter&gt; to quit\n\n&quot;</span>, TOPIC, CLIENTID, QOS);</div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#a9c1c28258f0d5c6a44ff53a98618f5f3">MQTTClient_subscribe</a>(client, TOPIC, QOS);</div><div class="line"></div><div class="line">    <span class="keywordflow">do</span></div><div class="line">    {</div><div class="line">        ch = getchar();</div><div class="line">    } <span class="keywordflow">while</span>(ch!=<span class="charliteral">&#39;Q&#39;</span> &amp;&amp; ch != <span class="charliteral">&#39;q&#39;</span>);</div><div class="line"></div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#a1e4d90c13a3c0705bc4a13bfe64e6525">MQTTClient_disconnect</a>(client, 10000);</div><div class="line">    <a class="code" href="_m_q_t_t_client_8h.html#ae700c3f5cfea3813264ce95e7c8cf498">MQTTClient_destroy</a>(&amp;client);</div><div class="line">    <span class="keywordflow">return</span> rc;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Thu Sep 13 2018 13:40:20 for Paho MQTT C Client Library by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.14
</small></address>
</body>
</html>
